package com.myidis.servlet;

import com.auxiliary.CICalculator;
import com.auxiliary.TimeSequence;
import com.myidis.entity.*;
import com.myidis.mapper.CIDIIndexManagementMapper;
import com.myidis.mapper.CIResultMapper;
import com.myidis.mapper.QuotaDataMapper;
import com.myidis.request.DICalculatorReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CICalculateServlet {
    @Autowired
    private QuotaDataMapper quotaDataMapper;
    @Autowired
    private CIDIIndexManagementMapper cidiIndexManagementMapper;
    @Autowired
    private CIResultMapper ciResultMapper;

    public ArrayList<IndexResultList> all() {
        return ciResultMapper.list();
    }

    public ArrayList<FullCIResult> getData(IndexResultList sea) {
        return ciResultMapper.getData(sea);
    }

    public FullCIResult getInfo(int sea) {
        return ciResultMapper.getInfo(sea);
    }

    public ArrayList<CIResult> calculate(DICalculatorReq sea) {


        //先行指标组合时间序列的获取
        //从传回的json中拿到指标名称，查找对应的指标
        ArrayList<TimeSequence> LeadQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> LeadDataSet = cidiIndexManagementMapper.getOne(sea.getCombination(), "先行");
        //通过查到的指标，以其名称取到具体数据并生成时间序列
        TimeSequence LeadTs = new TimeSequence();
        for (AnalysisIndex a: LeadDataSet){
            LeadTs.Rebuild(quotaDataMapper.getByQuota(a.getIndex(),sea.getStartTime(), sea.getEndTime()));
            LeadQuotaSet.add(LeadTs);
        }

        //一致指标组合时间序列的获取
        ArrayList<TimeSequence> CoinQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> CoinDataSet = cidiIndexManagementMapper.getOne(sea.getCombination(), "一致");
        TimeSequence CoinTs = new TimeSequence();
        for (AnalysisIndex a: CoinDataSet) {
            CoinTs.Rebuild(quotaDataMapper.getByQuota(a.getIndex(),sea.getStartTime(), sea.getEndTime()));
            CoinQuotaSet.add(CoinTs);
        }


        //滞后指标组合时间序列的获取
        ArrayList<TimeSequence> LagQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> LagDataSet = cidiIndexManagementMapper.getOne(sea.getCombination(), "滞后");
        TimeSequence LagTs = new TimeSequence();
        for (AnalysisIndex a: LagDataSet) {
            LagTs.Rebuild(quotaDataMapper.getByQuota(a.getIndex(), sea.getStartTime(), sea.getEndTime()));
            LagQuotaSet.add(LagTs);
        }


        //计算对应先行指标组合的扩散指数，一致，滞后指标组合的扩散指数计算同理
        CIResult LeadQuotaDIResult = new CICalculator(LeadQuotaSet,CoinQuotaSet,LagQuotaSet,sea.getSpringDay()).CalculateCI();

        //基准扩散指数计算
        ArrayList<TimeSequence> benchmarkSet = new ArrayList<>();
        benchmarkSet.add(new TimeSequence(quotaDataMapper.getByOne(sea.getCombination())));
        CIResult benchmarkCIResult = new CICalculator(benchmarkSet, sea.getSpringDay()).CalculateCI();




        CIResult finalResult = new CIResult(sea.getSubject(), LeadTs.getDateList(),LeadQuotaDIResult, sea.getSpringDay(), benchmarkCIResult.coinIndex);

        ArrayList<CIResult> results = new ArrayList<>();
        results.add(finalResult);
        results.add(benchmarkCIResult);

        //把计算结果注入数据库
        for(int i = 0; i < finalResult.getCount(); i++){
            String subject = finalResult.getSubject();
            String time = finalResult.getDates().get(i);
            int combination = sea.getCombination();
            String calculator = sea.getCalculator();
            String calTime = sea.getCalTime();
            double LeadCI = finalResult.leadIndex.get(i);
            double CoinCI = finalResult.coinIndex.get(i);
            double LagCI = finalResult.lagIndex.get(i);
            double benchmarkCI = benchmarkCIResult.coinIndex.get(i);
            String startTime = sea.getStartTime();
            String endTime = sea.getEndTime();
            int springDay = sea.getSpringDay();


            FullCIResult DataSql = new FullCIResult(subject,time,LeadCI,
                    CoinCI,LagCI,benchmarkCI);

            IndexResultList ListSql = new IndexResultList(subject,springDay,calTime,combination,calculator,startTime,endTime);
            ciResultMapper.insertDetail(DataSql);
            ciResultMapper.insertList(ListSql);
        }


        return results;
    }

    public int delect(int del) {
        ciResultMapper.deleteData(del);
        return ciResultMapper.delete(del);
    }



}
